<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>验证密码是否和散列值匹配</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.password-verify" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">password_verify</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.5.0, PHP 7)</p><p class="refpurpose"><span class="refname">password_verify</span> &mdash; <span class="dc-title">验证密码是否和散列值匹配</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.password-verify-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>password_verify</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$hash</code></span>
   ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   验证密码是否和指定的散列值匹配。
  </p>
  <p class="para">
   注意 <span class="function"><a href="password_hash.html" class="function">password_hash()</a></span> 返回的散列包含了算法、 cost 和盐值。
   因此，所有需要的信息都包含内。使得验证函数不需要储存额外盐值等信息即可验证哈希。
  </p>
  <p class="para">
   时序攻击（timing attacks）对此函数不起作用。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.password-verify-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<code class="parameter">password</code></dt>

    <dd>

     <p class="para">
      用户的密码。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">hash</code></dt>

    <dd>

     <p class="para">
      一个由 <span class="function"><a href="password_hash.html" class="function">password_hash()</a></span> 创建的散列值。
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.password-verify-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果密码和散列值匹配则返回 <strong><code>TRUE</code></strong>，否则返回 <strong><code>FALSE</code></strong> 。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.password-verify-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="openssl_spki_export.example.basic">
    <p><strong>Example #1 <span class="function"><strong>password_verify()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;想知道以下字符从哪里来，可参见&nbsp;password_hash()&nbsp;的例子<br /></span><span style="color: #0000BB">$hash&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'$2y$07$BCryptRequires22Chrcte/VlQH0piJtjXl.0t1XkA8pw9dMXTpOq'</span><span style="color: #007700">;<br /><br />if&nbsp;(</span><span style="color: #0000BB">password_verify</span><span style="color: #007700">(</span><span style="color: #DD0000">'rasmuslerdorf'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$hash</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Password&nbsp;is&nbsp;valid!'</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Invalid&nbsp;password.'</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Password is valid!
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.password-verify-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="password_hash.html" class="function" rel="rdfs-seeAlso">password_hash()</a> - 创建密码的散列（hash）</span></li>
    <li class="member"><a href="https://github.com/ircmaxell/password_compat" class="link external">&raquo;&nbsp;用户使用</a></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>